package com.group.itms.routes.stub;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

import com.group.itms.routes.json.JSONParser;

import android.os.AsyncTask;
import android.util.Log;


public class ObjectMapStub {

	private volatile MapDownloader mapDownloader;
	private static int downloadersCount = 0;
	
	private int id;
	private int object;
	private boolean isActive;
	private String mapUrl;
	private byte[] map;

	private float x1;
	private float y1;
	private float x2;
	private float y2;
	private float x3;
	private float y3;
	private float x4;
	private float y4;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public int getObject() {
		return object;
	}

	public void setObject(int object) {
		this.object = object;
	}

	public boolean isActive() {
		return isActive;
	}

	public void setActive(boolean isActive) {
		this.isActive = isActive;
	}

	public String getMapUrl() {
		return mapUrl;
	}

	public void setMapUrl(String mapUrl) {
		this.mapUrl = mapUrl;
	}

	public byte[] getMap() {
		return map;
	}

	public void setMap(byte[] map) {
		this.map = map;
	}

	public float getX1() {
		return x1;
	}

	public void setX1(float x1) {
		this.x1 = x1;
	}

	public float getY1() {
		return y1;
	}

	public void setY1(float y1) {
		this.y1 = y1;
	}

	public float getX2() {
		return x2;
	}

	public void setX2(float x2) {
		this.x2 = x2;
	}

	public float getY2() {
		return y2;
	}

	public void setY2(float y2) {
		this.y2 = y2;
	}

	public float getX3() {
		return x3;
	}

	public void setX3(float x3) {
		this.x3 = x3;
	}

	public float getY3() {
		return y3;
	}

	public void setY3(float y3) {
		this.y3 = y3;
	}

	public float getX4() {
		return x4;
	}

	public void setX4(float x4) {
		this.x4 = x4;
	}

	public float getY4() {
		return y4;
	}

	public void setY4(float y4) {
		this.y4 = y4;
	}
	
	public void startDownloading() {
		if (mapDownloader == null && mapUrl != null) {
			mapDownloader = new MapDownloader();
			mapDownloader.execute(mapUrl);
			synchronized (ObjectMapStub.class) {
				downloadersCount++;
			}
		}
	}

	public boolean isDownloading() {
		return mapDownloader != null;
	}
	
	public static synchronized int getDownloadersCount() {
		return downloadersCount;
	}
	
	@Override
	public String toString() {
		return "ObjectMapStub[id= " + id + "]";
	}
	
	class MapDownloader extends AsyncTask<String, Void, Void> {

		@Override
		protected Void doInBackground(String... params) {
			try {
				return downloadMap(params);
			} catch (Exception e) {
				Log.e("MapDownloader", "Can't download map", e);
				return null; 
			}
		}

		private Void downloadMap(String... params) {
			String rawUrl = params[0];
			JSONParser parser = JSONParser.getInstance();
			InputStream in;
			try {
				String url;
				int lsi = rawUrl.lastIndexOf('/') + 1;
				if (lsi != -1) {
					url = rawUrl.substring(0, lsi) 
						+ URLEncoder.encode(rawUrl.substring(lsi), "UTF-8");
					url = url.replaceAll("\\+", "%20");
				} else {
					url = rawUrl;
				}
				in = parser.getInputStream(url);
			} catch (IOException e) {
				Log.e("MapDownloader", "Unable to get inputStream", e);
				parser.release();
				return null;
			}
			
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int read;
			try {
				while ( (read = in.read(buffer)) != -1) {
					bos.write(buffer, 0, read);
				}
				map = bos.toByteArray();
			} catch (IOException e) {
				Log.e("MapDownloader", "Unable to read stream", e);
			} finally {
				parser.release();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			mapDownloader = null;
			synchronized (ObjectMapStub.class) {
				downloadersCount--;
			}
		}
		
	}
	
}
